<?php

//发布帖子
session_start();//开启session

/**
 * @var $config
 * @var $key
 */
include 'api.php';
include '../WeSql/WeSql.php';
include '../WeSql/config.php';
include '../WeSql/widget.php';

/** @var 账号 $_REQUEST */
@$user = $_REQUEST['user'];
/** @var 密码 $_REQUEST */
@$pass = $_REQUEST['pass'];

/** @var 标题 $_REQUEST */
@$title = addslashes($_REQUEST['title']);

/** @var 内容 $content */
@$content = addslashes($_REQUEST['content']);
/**@var 封面 $cover*/
@$cover = addslashes($_REQUEST['cover']);

/** @var 分类 $class */
@$class = $_REQUEST['class'];

@$weSql = new WeSql($config);

/**
 * 处理ip拉黑问题
 */
isIpBlack($weSql, getIP());
if($title == '' or $content == '' or $cover == '' or $user == '' or $pass == ''){
    $code = -1;
    $msg = "不能留空";
    $exception = '';
}else{

    if(!preg_match("/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is",$cover)){
        $code = -1;
        $msg = "封面链接格式错误";
        $exception = '';
        @$result = [
            'code' => $code,
            'msg' => $msg,
            'exception' => $exception
        ];
        exit(json_encode($result,JSON_UNESCAPED_UNICODE));
    }

    if(isUser($weSql,$user)){

        isUserBlack($weSql,$user);

        isPassWord($weSql,$user,$pass);
        /** @var $IS_PUT_FORUM */
        /** @var $FORUM_PUT_TIME */
        /** @var $STATE_AUDIT */
        if($IS_PUT_FORUM){
            if(!isset($_SESSION['FORUM_PUT_TIME']) or time() - $_SESSION['FORUM_PUT_TIME'] > $FORUM_PUT_TIME){
                $_SESSION['FORUM_PUT_TIME'] = time();

                if($weSql->insert("forum",[
                    'state' => $STATE_AUDIT?1:0,
                    'user' => $user,
                    'title' => $title,
                    'content' => $content,
                    'cover' => $cover,
                    'time' => time()
                ])){
                    $code = 1;
                    $msg = "发布成功";
                    $exception = "发帖成功";
                }else{
                    $code = -1;
                    $msg = "发布失败";
                    $exception = "发帖失败";
                }

            }else{
                $code = -1;
                $msg = "请间隔".($FORUM_PUT_TIME / 60)."分钟后再次发布";
                $exception = "发帖失败";
            }
        }else{
            $code = -1;
            $msg = "发帖通道已经关闭";
            $exception = "发帖失败";
        }
    }else{
        $code = -1;
        $msg = "账号不存在";
        $exception = "数据库里信息找不到该记录账号";
    }
}

/** @var $code
 * @var $msg
 * @var $exception
 */
@$result = [
  'code' => $code,
  'msg' => $msg,
  'exception' => $exception
];
echo json_encode($result,JSON_UNESCAPED_UNICODE);
?>